<?


JCAT_Package::ImportClass( ROOT_PATH.'Classes/Coder/class.CoderDocBase.php') ;

class UICtrlGroupDoc extends UICtrlDocBase 
{
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @param	$sPath=''	string	What's this Parameter ?
	 * @return	array
	 */
	public function UICtrlGroupDoc($sPath='')
	{
		$this->UICtrlDocBase( __CLASS__, $sPath ) ;
		
		// 初始化文档结构
		// $this->Set('Children',array(),false) ;
		$aSubDoc = new UICtrlContainerDoc('UICtrlChildren') ;
		$this->AddDocument($aSubDoc,'UICtrlChildren') ;
	}

	public function GetUICtrlChildren()
	{
		return $this->GetDocument('UICtrlChildren') ;
	}


	/**
	 * what's this
	 * 
	 * @access	public
	 * @static 
	 * @return	string
	 */
	static public function GetControllerClassName()
	{
		return preg_replace('/Doc$/','',__CLASS__) ;
	}

	/**
	 * 将序列化以后的内容存入到 文档
	 * 
	 * @access	protected
	 * @param	$sPath				文件路径
	 * @param	$sByteContents		字节内容
	 * @param	$bAsBinary			作为二进制存储
	 * @return	bool
	 */
	protected function SaveToFile( $sPath, $sByteContents, $bAsBinary=false )
	{
		// 调用父类
		parent::SaveToFile( $sPath, $sByteContents, $bAsBinary ) ;
		
		// 产生 设置选项 代码
		if( $this->GetImplementType()==UICtrlDocBase::IMPTYPE_DERIVATION )
		{
			$this->WirteUnmodifiableBlock($sPath,'Children',$this->CreateCode_Children(),CoderDocBase::UnmodifiableBlockKind_Code,2) ;
		}
		else
		{
			$this->WirteUnmodifiableBlock($sPath,'Children',$this->CreateCode_Children(),CoderDocBase::UnmodifiableBlockKind_Code,0) ;
		}
		
		// 完成 返回 
		return true ;
	}


	/**
	 * What's this Method ?
	 * 
	 * @access	protected
	 * @return	void
	 */
	protected function CreateNewDocument()
	{
		// 创建 新的文件 内容 :::::::::::::::::::
		$aTemplate = new JCAT_UIHtml() ;
		$aTemplate->SetVar('TheDoc',$this) ;
		
		// 模版文件
		$sTemplateFile = dirname(__FILE__).'/template.UICtrlGroup.php' ;
		
		// 产生内容
		$sNewSource = $aTemplate->Display( $sTemplateFile,false) ;
		
		// 调用 父类 创建一个空文件
		parent::CreateNewDocument() ;
		
		// 写入新文件
		file_put_contents($this->GetPath(),$sNewSource) ;
	}


	
	public function CreateCode_IncludeParentClass()
	{
		$sParentClassName = $this->Get('UICtrlKind') ;
		return "JCAT_Package::ImportClass('{$sParentClassName}',JCAT::PACKAGE_UICTRLGTK) ;" ;
	}

	public function CreateCode_Children()
	{
		$aSubDoc = $this->GetDocument('UICtrlChildren') ;
		$arrOptions = $aSubDoc->Get('Children') ;
		if( !$arrOptions )
		{
			return '' ;
		}
		
		$arrCodeLines = array() ;
		$sVarName = $this->GetImplementType()==UICtrlDocBase::IMPTYPE_DERIVATION?
				'		$this' :
				'$aNewInstance' ;
		
		JCAT_Package::ImportClass('JCAT_ArrayIterator',JCAT::PACKAGE_PATTERN) ;
		$aIterator = new JCAT_ArrayIterator($arrOptions) ;
		$aIterator->First() ;
		while( !$aIterator->IsDone() )
		{
			$arrOptionInfo = $aIterator->Current() ;

			$sText = str_replace('"','\\"',$arrOptionInfo[LstOptions::CLM_TEXT]) ;
			$sValue = str_replace('"','\\"',$arrOptionInfo[LstOptions::CLM_VALUE]) ;
			if($sValue=='*')
			{
				$sValue = $sText ;
			}

			//$arrCodeLines[] = "{$sVarName}->AddOption(\"{$sText}\",\"{$sValue}\",{$sSelected}) ;" ;

			$aIterator->Next() ;
		}
		
		return implode("\r\n",$arrCodeLines) ;
	}
	
}

JCAT_Package::ImportClass('JCAT_UIHtml',JCAT::PACKAGE_UIHTML) ;
?>